<?php
class CacheSession extends Session {
    protected $sessionNS = 'Session';
    protected function __construct($sessionNS = DEFAULT_SESSION_NS) {
        $this->sessionNS = $sessionNS;
    }
    
    public function open() {
    }
    public function close() {}
    
    
    public function read() {
        $sessionId = session_id();
        $_SESSION = Cache::get($this->sessionNS, $sessionId);
    }
    
    public function write() {
        $sessionId = session_id();
        $updateTime = time();
        $lifeTime = Config::get('SESSION_LIFETIME');
        $lifeTime = $lifeTime >= 86400 ? $lifeTime : 86400;
        Cache::set($this->sessionNS, $sessionId, $_SESSION, $lifeTime);
    }
    
    public function sessDestroy() {
        $_SESSION = array();
        Cache::delete($this->sessionNS, session_id());
    }
    
    public function gc() {
        if ('file' == strtolower(Config::get('CACHE_ENGINE'))) {
            @chdir(SESS_DIR);
            $sessFileList = glob('*.php');
            $time = time();
            $gcMaxLife = Config::get('SESSION_LIFETIME');
            $gcMaxLife = $gcMaxLife >= 86400 ? $gcMaxLife : 86400;
            foreach($sessFileList as $file) {
                if($time - filemtime($file) > $gcMaxLife){
                    @unlink($file);
                }
            }
        }
    }
    
    public static function delete($sessionId) {
        
    }
};
?>